﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class TopicsBLL : CommonBLL<TopicsInfo, TopicsDAL>
    {
        public static TopicsInfo Get(int id)
        {
            using (var dal = new TopicsDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static IQueryable<TopicsInfo> GetList(int fid, bool? isDigest, string search)
        {
            var re = GetList();
            re = re.Where(o => o.Fid == fid);
            if (isDigest.HasValue)
                re = re.Where(o => o.IsDigest);
            if (!string.IsNullOrEmpty(search))
                re = re.Where(o => o.Title.Contains(search));
            return re;
        }

        public static int GetCount(int studentId, DateTime startTime, DateTime endTime)
        {
            var list = GetList();
            return
                list.Count(
                    o =>
                    o.PosterId == studentId && o.PosterType == 1 && o.PostTime >= startTime && o.PostTime <= endTime);
        }

        public static bool Del(int id)
        {
            using (var dal = new TopicsDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

        public static bool UpdateView(int id)
        {
            using (var dal = new TopicsDAL())
            {
                return dal.UpdateView(id);
            }
        }
    }
}
